<?php

namespace Modules\Shop\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Shop\Entities\Tag;
use Modules\Shop\Entities\TagArticle;
use Modules\Shop\Http\Requests\TagRequest;

class TagController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
    	$tags=Tag::get();
        return view('shop::tag.index',compact('tags'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('shop::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(TagRequest $tagRequest)
    {
    	Tag::create($tagRequest->all());
    	session()->flash('success','标签添加成功！');
    	return redirect ()->back();
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('shop::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {

        return view('shop::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(TagRequest $tagRequest,$id)
    {
    	$tag=Tag::find($id);
    	$tag->name=$tagRequest->input('name');
    	$tag->save();
    	session()->flash('success','编辑成功！');
    	return redirect ()->back();
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
    	//判断该标签是否有被文章引用，如果有就不能删除
    	$a=TagArticle::where('tag_id',$id)->first();
    	if($a){
    		session()->flash('danger','该标签已被文章使用，不能删除！');
    		return redirect ()->back();
		}
    	Tag::destroy ($id);
    	session()->flash('success','标签删除成功！');
    	return redirect ()->back();
    }
}
